package com.itcg.springboot03deptemptest.controller;

import com.itcg.springboot03deptemptest.pojo.Emp;
import com.itcg.springboot03deptemptest.pojo.EmpC;
import com.itcg.springboot03deptemptest.pojo.Page;
import com.itcg.springboot03deptemptest.pojo.Result;
import com.itcg.springboot03deptemptest.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.OffsetDateTime;


@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @RequestMapping("/updates")
    public Result updates(@RequestBody Emp emp) {
        if (emp == null) {
            return Result.error("参数错误");
        }
        empService.updates(emp);
        return Result.success();
    }

    @RequestMapping("/inserts")
    public Result inserts(@RequestBody Emp emp) {
        try {
            if (emp == null) {
                return Result.error("参数错误");
            }
            int i = empService.inserts(emp);
            if (i > 0) {
                return Result.success(i);
            }
            return Result.error("添加失败");
        } catch (Exception e) {
            return Result.error("添加失败");
        }
    }

    @RequestMapping("/deletes")
    public Result deletes(@RequestBody Integer[] ids) {
        if (ids == null) {
            return Result.error("参数错误");
        }
        empService.deletes(ids);
        return Result.success();
    }

    @RequestMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.error("参数错误");
        }
        Emp emp = empService.findById(id);
        if (emp == null) {
            return Result.error("查无此人");
        }
        return Result.success(emp);
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody EmpC empC,
                           @RequestParam(name = "pageSize", required = false, defaultValue = "3") Integer pageSize,
                           @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum) {
        //调用业务层的方法查询当前分页Page对象
        Page<Emp> page = empService.findByCondition(empC, pageNum, pageSize);
        return Result.success(page);
    }
}
















